package com.example.lt.controller.admin;

import com.example.lt.entity.dto.LoginDTO;
import com.example.lt.entity.user.User;
import com.example.lt.entity.vo.LoginVO;
import com.example.lt.service.LoginService;
import com.example.lt.util.JwtUtil;
import com.example.lt.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 小涛Tao
 * @Date: 2024/08/03/22:57
 * version 1.0 注释：
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/luckyjourney/login")
public class AdminLoginController {

    private final LoginService loginService;

    /**
     * 管理员登录
     * @param loginDTO
     * @return
     */
    @PostMapping
    public Result login(@RequestBody @Validated LoginDTO loginDTO) {
        log.info("管理员登录：{}", loginDTO.getEmail());
        User user = loginService.login(loginDTO);
        // 生成token
        String token = JwtUtil.getJwtToken(user.getId(), user.getNickName());
        // 返回登录结果
        LoginVO loginVO = LoginVO.builder().name(user.getNickName()).sex(user.getSex()).token(token).build();
        return Result.ok().data(loginVO);
    }

}
